
mod frame;
pub mod poll_ack;
pub use frame::Frame;
pub mod address;
pub mod cmd;
mod codec;

pub use codec::Proto;
pub use codec::BodyDecode;
pub use codec::BodyEncode;
pub use codec::Cmd;
pub use codec::ToProtoBytes;

pub const HEAD0: u8 = 0xE1;
pub const HEAD1: u8 = 0x1E;
pub const HEAD: u16 = 0xE11E;
pub const END: u8 = 0xEF;

pub const TYPE_SIMPLE_REQ: u8 = 0x00;
pub const TYPE_SIMPLE_RES: u8 = 0x01;

pub const TYPE_REQ: u8 = 0x02;
pub const TYPE_REQ_ACK: u8 = 0x03;

pub const TYPE_RES: u8 = 0x04;
pub const TYPE_RES_ACK: u8 = 0x05;

pub const TYPE_NOTIFY: u8 = 0x06;

pub const TYPE_PING: u8 = 0x07;
pub const TYPE_PONG: u8 = 0x08;

pub const TYPE_CONNECT: u8 = 0x09;
pub const TYPE_CONNECT_ACK: u8 = 0x0A;

pub const TYPE_POLL: u8 = 0x0B;
pub const TYPE_POLL_ACK: u8 = 0x0C;

pub const TYPE_LOG: u8 = 0x0D;

pub fn xor_sum(buf: &[u8]) -> u8 {
    let mut s = 0;
    for v in buf {
        s = s ^ *v;
    }
    s
}

pub fn make_empty(ty: u8, seq: u8, src: u8, dst: u8) -> [u8; 9] {
    let mut buf = [HEAD0, HEAD1, 9, ty, seq, src, dst, 0, END];
    let sum = xor_sum(&buf[3..7]);
    buf[7] = sum;

    buf
}

